package com.fetch.listener;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JTextField;

import com.download.utils.DownloadHelper;
import com.download.utils.LogUtil;

public class UrlFocusAdapter extends FocusAdapter {
    
    private JTextField url;
    private JTextField fileName;
    
    public UrlFocusAdapter(JTextField url, JTextField fileName) {
        super();
        this.url = url;
        this.fileName = fileName;
    }

    @Override
    public void focusLost(FocusEvent e) {
        String fileUrl = url.getText().trim();
        url.setText(fileUrl);
        //校验url是否合法
        if(DownloadHelper.isUrl(fileUrl)){
            String filename = DownloadHelper.getFileNameByUrl(fileUrl);
            if(!filename.equals(fileName.getText())){
                fileName.setText(filename);
            }
        } else {
            LogUtil.info("Url不合法！");
        }
    }
}
